//
//  YAxisView.m
//  ZXT
//
//  Created by 张逸阳 on 2017/8/16.
//  Copyright © 2017年 dr. All rights reserved.
//

#import "YAxisView.h"
#define xAxisTextGap 5 //x轴文字与坐标轴间隙
#define yAxisTextGap 5 //y轴文字与坐标轴间隙

#define numberOfYAxisElements 5 // y轴分为几段

#define kChartTextColor         [UIColor whiteColor]
#define kYLineColor             [UIColor greenColor]
@interface YAxisView()
@property (assign, nonatomic) CGFloat yMax;
@property (assign, nonatomic) CGFloat yMin;
@end

@implementation YAxisView
- (instancetype)initWithFrame:(CGRect)frame yMax:(CGFloat)yMax yMin:(CGFloat)yMin
{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithRed:85.0f/255.0f green:85.0f/255.0f blue:85.0f/255.0f alpha:1.0f];
        self.yMax = yMax;
        self.yMin = yMin;
    }
    return  self;
}

- (void)drawRect:(CGRect)rect
{
    CGContextRef context = UIGraphicsGetCurrentContext();
    //计算坐标轴的位置以及大小
    NSDictionary *attr = @{NSFontAttributeName:[UIFont systemFontOfSize:8]};
    CGSize labelSize = [@"x" sizeWithAttributes:attr];
    
    // x轴下文字高度 和 间距
    CGFloat xAxisText = labelSize.height + xAxisTextGap;
    [self drawLine:context startPoint:CGPointMake(self.frame.size.width-1, 0) endPoint:CGPointMake(self.frame.size.width-1, self.frame.size.height - xAxisText) lineColor:kYLineColor lineWidth:1];
    
    NSDictionary *hKLabelAttr = @{NSFontAttributeName:[UIFont systemFontOfSize:8]};
    
    NSString *yLabelText = @"回款";
    CGSize hKLabel = [yLabelText sizeWithAttributes:hKLabelAttr];
    CGRect hKLabelRect = CGRectMake(self.frame.size.width-1-yAxisTextGap-hKLabel.width, 0, hKLabel.width, hKLabel.height);
    [yLabelText drawInRect:hKLabelRect withAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:8],NSForegroundColorAttributeName:kChartTextColor}];
    
    
    // y轴上所有label 占居的高度
    CGFloat allYLabrlHeight = self.frame.size.height - xAxisText ;
    // Label之间的间隙
    CGFloat labelMargin = (allYLabrlHeight + labelSize.height - (numberOfYAxisElements + 1) * labelSize.height) / numberOfYAxisElements;
    
    for (int i = 0; i<= numberOfYAxisElements; i++) {
        CGFloat avgValue = (self.yMax - self.yMin) / numberOfYAxisElements;
        if ([self isPureFloat:self.yMin + avgValue *i] ) {
            CGSize yLabelSize = [[NSString stringWithFormat:@"%.2f", self.yMin + avgValue * i] sizeWithAttributes:hKLabelAttr];
            
            
            
            [[NSString stringWithFormat:@"%.2f", self.yMin + avgValue * i] drawInRect:CGRectMake(self.frame.size.width - 1-yAxisTextGap - yLabelSize.width, self.frame.size.height - labelSize.height - 5 - labelMargin* i - yLabelSize.height/2, yLabelSize.width, yLabelSize.height) withAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:8],NSForegroundColorAttributeName:kChartTextColor}];
        }
        else {
            CGSize yLabelSize = [[NSString stringWithFormat:@"%.0f", self.yMin + avgValue * i] sizeWithAttributes:hKLabelAttr];
            
            [[NSString stringWithFormat:@"%.0f", self.yMin + avgValue * i] drawInRect:CGRectMake(self.frame.size.width - 1-5 - yLabelSize.width, self.frame.size.height - labelSize.height - 5 - labelMargin* i - yLabelSize.height/2, yLabelSize.width, yLabelSize.height) withAttributes:@{NSFontAttributeName :[UIFont systemFontOfSize:8],NSForegroundColorAttributeName:kChartTextColor}];
        }
        
        
    }
    
    
}


- (void)drawLine:(CGContextRef)context startPoint:(CGPoint)startPoint endPoint:(CGPoint)endPoint lineColor:(UIColor *)lineColor lineWidth:(CGFloat)width
{
    CGContextSetShouldAntialias(context, YES ); //抗锯齿
    CGColorSpaceRef Linecolorspace1 = CGColorSpaceCreateDeviceRGB();
    CGContextSetStrokeColorSpace(context, Linecolorspace1);
    CGContextSetLineWidth(context, width);
    CGContextSetStrokeColorWithColor(context, lineColor.CGColor);
    CGContextMoveToPoint(context, startPoint.x, startPoint.y);
    CGContextAddLineToPoint(context, endPoint.x, endPoint.y);
    CGContextStrokePath(context);
    CGColorSpaceRelease(Linecolorspace1);
}

/// 判断是小数还是整数
- (BOOL)isPureFloat:(CGFloat)num
{
    int i = num;
    
    CGFloat result = num - i;
    
    // 当不等于0时，是小数
    return result != 0;
}
@end
